package com.abm.data;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.abm.dbclases.Category;
import com.abm.dbclases.Client;
import com.abm.dbclases.Product;
import com.abm.dbclases.ProductLog;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;


public class DataSQLiteHelper extends OrmLiteSqliteOpenHelper {

	public static final String DATABASE_NAME = "stockdb.db";
	@SuppressLint("SdCardPath")
	public static final String DB_PATH = "/sdcard/stock/db/";
	public static final String DATABASE_NAME_FULLPATH = DB_PATH + DATABASE_NAME;

	// any time you make changes to your database objects, you may have to
	// increase the database version
	private static final int DATABASE_VERSION = 4;
	
	// the DAO object we use to access the table
	private Dao<Product, Integer> productDao;
	private Dao<Client, Integer> clientDao;
	private Dao<ProductLog, Integer> productLogDao;
	private Dao<Category, Integer> categoryDao;

	public DataSQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource conections) {
		try {

			TableUtils.createTable(connectionSource, Product.class);
			TableUtils.createTable(connectionSource, Client.class);
			TableUtils.createTable(connectionSource, ProductLog.class);
			TableUtils.createTable(connectionSource, Category.class);

		} catch (Exception e) {
			Log.e(DataSQLiteHelper.class.getName(), "Can't create database", e);
			throw new RuntimeException(e);
		}

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
			int oldVersion, int newVersion) {
		try {
			TableUtils.dropTable(connectionSource, Product.class, true);
			TableUtils.dropTable(connectionSource, Client.class, true);
			TableUtils.dropTable(connectionSource, ProductLog.class, true);
			TableUtils.dropTable(connectionSource, Category.class, true);
			
			TableUtils.createTable(connectionSource, Product.class);
			TableUtils.createTable(connectionSource, Client.class);
			TableUtils.createTable(connectionSource, ProductLog.class);
			TableUtils.createTable(connectionSource, Category.class);
			
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} catch (java.sql.SQLException e) {
			e.printStackTrace();
		}
	}

	public Dao<Product, Integer> getProductDao() {
		if (productDao == null) {
			try {
				productDao = getDao(Product.class);
			} catch (java.sql.SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return productDao;
	}
	

	public boolean updateProduct(Product p) {
		boolean ret = false;
		if (productDao != null) {
			try {
				productDao = getDao(Product.class);

				if (productDao.update(p) != 1) {
					ret = false;
				} else {
					productDao.refresh(p);
					ret = true;
				}

				productDao.refresh(p);

			} catch (Exception e) {
				ret = false;
				e.printStackTrace();
			}
		}
		return ret;
	}

	
	
	public Dao<Client, Integer> getClientDao() {
		if (clientDao == null) {
			try {
				clientDao = getDao(Client.class);
			} catch (java.sql.SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return clientDao;
	}
	

	public boolean updateClient(Client p) {
		boolean ret = false;
		if (clientDao != null) {
			try {
				clientDao = getDao(Client.class);

				if (clientDao.update(p) != 1) {
					ret = false;
				} else {
					clientDao.refresh(p);
					ret = true;
				}

				clientDao.refresh(p);

			} catch (Exception e) {
				ret = false;
				e.printStackTrace();
			}
		}
		return ret;
	}
	
	
	
	
	public Dao<ProductLog, Integer> getProductLogDao() {
		if (productLogDao == null) {
			try {
				productLogDao = getDao(ProductLog.class);
			} catch (java.sql.SQLException e) {
				e.printStackTrace();
			}
		}
		return productLogDao;
	}
	
	
	
	
	public Dao<Category, Integer> getCategoryDao() {
		if (categoryDao == null) {
			try {
				categoryDao = getDao(Category.class);
			} catch (java.sql.SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return categoryDao;
	}
	

	public boolean updateCategory(Category p) {
		boolean ret = false;
		if (categoryDao != null) {
			try {
				categoryDao = getDao(Category.class);

				if (categoryDao.update(p) != 1) {
					ret = false;
				} else {
					categoryDao.refresh(p);
					ret = true;
				}

				categoryDao.refresh(p);

			} catch (Exception e) {
				ret = false;
				e.printStackTrace();
			}
		}
		return ret;
	}
	
	
	
	
	
	
	/**
	 * Close the database connections and clear any cached DAOs.
	 */
	@Override
	public void close() {
		super.close();
	}
}
